# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Eric Bélanger <eric@archlinux.org>

pkgbase=flac
pkgname=('flac' 'flac-doc')
pkgver=1.4.2
pkgrel=1
pkgdesc='Free Lossless Audio Codec'
url='https://xiph.org/flac/'
arch=('x86_64')
license=('BSD' 'GPL')
depends=('gcc-libs' 'libogg')
makedepends=('nasm' 'cmake' 'ninja' 'doxygen' 'git' 'pandoc')
options=('debug')
_commit=b32e5cbf9818ca23dd22aaa75522042c16ea7d17  # tags/1.4.2
source=("git+https://github.com/xiph/flac?signed#commit=$_commit")
b2sums=('SKIP')
validpgpkeys=('5DE3E0509C47EA3CF04A42D34AEE18F83AFDEB23') # https://github.com/web-flow.gpg

pkgver() {
  cd flac
  git describe --tags | sed 's/[^-]*-g/r&/;s/-/+/g'
}

prepare() {
  cd flac

  # Fix GStreamer
  # https://bugs.archlinux.org/task/76120
  git cherry-pick -n 3d55a9dc6c14317b4766db96bb52e2fd95e920a9

  # Shorten tests
  sed -i 's/FLAC__TEST_LEVEL=1/FLAC__TEST_LEVEL=0/' test/CMakeLists.txt
}

build() {
  cmake -S flac -B build -G Ninja \
    -DCMAKE_BUILD_TYPE=None \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DBUILD_SHARED_LIBS=ON \
    -DINSTALL_MANPAGES=ON \
    -DWITH_STACK_PROTECTOR=OFF \
    -DNDEBUG=ON
  cmake --build build

  # Build docs and place them where install expects them
  cmake --build build --target FLAC-doxygen
  mv build/doc/doxytmp/html flac/doc/api
}

check() {
  cd build
  ctest --output-on-failure --stop-on-failure -j1
}

package_flac() {
  provides=('libFLAC.so' 'libFLAC++.so')

  DESTDIR="${pkgdir}" cmake --install build

  install -Dm 644 flac/src/*/*.m4 -t "${pkgdir}/usr/share/aclocal"

  mkdir -p doc/usr/share
  mv {"${pkgdir}",doc}/usr/share/doc

  install -Dm 644 flac/COPYING.Xiph -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

package_flac-doc() {
  pkgdesc+=' documentation'
  depends=()

  mv doc/* "$pkgdir"

  install -Dm 644 flac/COPYING.Xiph -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

# vim:set sw=2 sts=-1 et:
